#ifndef XDIVA_NODEITEM_H
#define XDIVA_NODEITEM_H

#include "AbstractShapeItem.h"

class NodeManager;
class xMappingTree;
class QAction;
class xMappingNode;
class DIVA_VM;

class NodeItem: public AbstractShapeItem {
	Q_OBJECT

public:
	NodeItem(std::string, xMappingTree*, AbstractManager* );
	NodeItem(DIVA_VM *, xMappingTree*, AbstractManager* );
	NodeItem(xMappingNode*, AbstractManager*);
	virtual ~NodeItem();

	virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
	virtual QRectF boundingRect() const;
	virtual int height();
	virtual void postPaint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget );
	virtual bool validate() const;

	const static int WIDTH  = 150;				// default width
	const static int NODE_NAME_HEIGHT = 25;		// 顯示 node name部分的色塊高度
	const static int NODE_BODY_HEIGHT = 20;		// 顯示 node port部分的色塊高度
	const static int OVERLAP_HEIGHT = 15;		// 兩個色塊重疊部分的高度
	const static int ROUND_RADIUS = 5;			// 圓角的半徑
	
	void resetSizeAndPortPosition();
	std::vector<bool>& getTreeItemExpandedState(QString);
	void setTreeItemExpandedState(QString, int, bool);
	NodeInPort* addInPort();
	NodeOutPort* addOutPort();
	void deleteInPort( NodeInPort* );
	void deleteOutPort( NodeOutPort* );
	void removeAllLinks();
	bool isPortListChanged();
	void setPortListChanged(bool);
	NodeInPort * getInPortByName( const std::string & name ) const;
	
protected:
	QColor _nameColor;
	QColor _bodyColor;
	void mousePressEvent(QGraphicsSceneMouseEvent* );
	void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
	void keyPressEvent(QKeyEvent*);
	void contextMenuEvent(QGraphicsSceneContextMenuEvent* );
	QVariant itemChange(GraphicsItemChange ,const QVariant &);
	void updateLinePosition();
	void setPortPosition();
	void createColor (std::string);
	void setupPort();
	
signals:
	void itemSelected(NodeItem* );
	void itemDeSelected();
	void portListChanged(NodeItem* );

public slots:
	void editFormula();
	void editCustomString();
	void remove();

private:

	std::vector<bool> _inportTreeItemExpandedState;
	std::vector<bool> _outportTreeItemExpandedState;
	std::vector<bool> _attributeTreeItemExpandedState;
	bool _portListChanged;
	QAction* _deleteAction;
	QAction* _editFormulaAction;
	QAction* _editCustomStringAction;
	QMenu* _contextMenu;
	void createActions();
	void initMenu();
	void initNode( std::string );
};

#endif
